//
//  DXUserAccountViewModel.swift
//  weiBoOfSwift
//
//  Created by simon on 15/12/8.
//  Copyright © 2015年 simon. All rights reserved.
//

import UIKit

let file = keyedArchiverPath("userInfo.archver")
class DXUserAccountViewModel: NSObject {

    var userAccount:DXAccountModel?
//    判断是否登录
    var isLogIn:Bool {
        return access_token != nil
    }
//    返回token值
    var access_token:String?{
        guard let token = userAccount?.access_token else{
         return nil
        }
        if isexpires == true {
            return nil
        }else{
        return token
        }
    }
//    是否过期的判断
//    true--->过期, 
//    false -->未过期
    var isexpires:Bool {
//  过期日期是空,则返回过期
        guard let expireDate = userAccount?.expires_date else{
        return true
        }
    let result = NSDate().compare(expireDate)
//        OrderedAscending 升序
//        OrderedDescending 降序
        if result != .OrderedAscending{
//        过期
            return true
        }else{
            
        return false
        }
    }
    
    
override init(){
    super.init()
    userAccount = getUserInfo()
//    logPrint(userAccount)
    
    }
//    懒加载
 static let shareUserAccount:DXUserAccountViewModel = DXUserAccountViewModel()
    
    
}


extension DXUserAccountViewModel{
 
// MARK: 归档
    func saveUserInfo(account:DXAccountModel)->() {
        logPrint(account)
        
       let isSucess =  NSKeyedArchiver.archiveRootObject(account, toFile:file)
        
//        logPrint(file)
        if isSucess {
        logPrint("归档成功")
        }else{
//     归档失败
//        logPrint("归档失败")
        
        }
        
            }
// MARK:  解档
     func getUserInfo()->(DXAccountModel)?{
        //         let file = DXTools.keyedArchiverPath("userInfo")
        return  NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? DXAccountModel
    }


}

// MARK:授权相关的网络授权
extension DXUserAccountViewModel{
// MARK: - 网络请求获取token值
     func loadUserToken(code code:String, loadSuccess:(()->())?,loadFailure:((error:NSError)->())?){
        
        DXNetWorkClient.loadToken(code: code, sucessClosure: { (responser) -> () in
//        token请求成功,创建 DXAccountModel数据模型
            let account = DXAccountModel(dict: responser as! [String : AnyObject])
 //     logPrint(account)
 //      根据token值获取用户信息,并将account赋值给viewmodel中account
            self.userAccount = account
            
            self.loadUserInfo(account, loadSuccess: loadSuccess, loadFailure: loadFailure)
            }) { (error) -> () in
//        失败回调
                loadFailure!(error: error)
        }
    }
    
// MARK: - 网络请求用户信息
    func loadUserInfo(account:DXAccountModel,loadSuccess:(()->())?,loadFailure:((error:NSError)->())?){
        
        DXNetWorkClient.loadUserInfo(account, sucessClosure: { (responser) -> () in
//                    获取到用户信息,将头像和昵称保存在模型中
            let infoDict = responser as! [String:AnyObject]
            
//            logPrint(infoDict)
            
            self.userAccount!.avatar_large = infoDict["avatar_large"] as? String
            self.userAccount!.screen_name = infoDict["screen_name"]  as? String
//            保存数据
            self.saveUserInfo(self.userAccount!)
            
//            成功回调
            loadSuccess!()
            
            }) { (error) -> () in
                loadFailure!(error: error)
//                logPrint("获取到用户信息\(error)")
        }
        
    }
}